// main for books version of contract program as of Chapter 8, Section II
// file: ch8tst3.cpp
#include <iostream.h>
#include "contract.h"
const MAX_CONTRACTS = 5;
typedef Contract ContractArray[MAX_CONTRACTS];
char GetProperty();
char GetOperatorChoice();
int GetContractID();
void DisplayInfo(Contract contract);
void ChangeInfo(Contract& contract);
void DisplayClosingMessage();
void DisplayIntro();
void DisplayAll (ContractArray contracts);
int GetSquareFootage();
int GetNumDesks();
int GetNumDays();
void ProcessUserChoices();
void InitializeInstances(ContractArray contracts);
void main()
{
DisplayIntro();
ProcessUserChoices();
DisplayClosingMessage();
}
char GetOperatorChoice()
{ char choice;
cout << "Please choose one of the following operations\n";
cout << " 'D' to display the information for a contract\n";
cout << " 'L' to display the information on ALL contracts\n";
cout << " 'C' to change the information for a contract\n";
cout << " 'Q' to quit the program\n";
cin >> choice;
return choice;
}
int GetContractID()
{ int num;
cout << "\nPlease choose which contract (1 through 5) that you wish to work with\n";
cout << "Choose 0 (zero) when you are finished with this operation.\n";
cin >> num;
return num;
}
void ChangeInfo(Contract& contract)
{ char property;
property = GetProperty();
while ((property != 'N') && (property != 'n'))
{ switch (property)
{ case 'S':
case 's':
int sqFootage;
sqFootage = GetSquareFootage();
contract.ChangeSquareFootage(sqFootage);
break;
case 'K':
case 'k':
int numDesks;
numDesks = GetNumDesks();
contract.ChangeNumDesks(numDesks);
break;
case 'Y':
case 'y':
int numDays;
numDays = GetNumDays();
contract.ChangeNumDays(numDays);
break;
default:
cout << "\nInvalid choice of properties. Please try again\n";
} // end switch
property = GetProperty();
}
}
void DisplayInfo(Contract contract)
{
int sqFootage;
sqFootage = contract.ProvideSquareFootage();
cout << "\n\nThe square footage for the contract is: " << sqFootage << endl;
int numDesks;
numDesks = contract.ProvideNumberOfDesks();
cout << "The number of desks for the contract is: " << numDesks << endl;
int numDays;
numDays = contract.ProvideNumberOfDays();
cout << "The number of days for the contract is: " << numDays << endl;
double charge;
charge = contract.ProvidePerWeekCharge();
cout << "The per week charge for the contract is: " << charge << endl;
cout << endl << endl;
}
void DisplayAll (ContractArray contracts)
{ int sqFootage;
int numDesks;
int numDays;
double charge;
cout << endl << endl; // To set off the output
for (int contract = 0; contract < MAX_CONTRACTS; contract++)
{ sqFootage = contracts[contract].ProvideSquareFootage();
cout << "The square footage for contract " << (contract+1) << " is: "
<< sqFootage << endl;
numDesks = contracts[contract].ProvideNumberOfDesks();
cout << "The number of desks for contract " << (contract+1) << " is: "
<< numDesks << endl;
numDays = contracts[contract].ProvideNumberOfDays();
cout << "The number of days for contract " << (contract+1) << " is: "
<< numDays << endl;
charge = contracts[contract].ProvidePerWeekCharge();
cout << "The per week charge for contract " << (contract+1) <<" is: "
<< charge << endl;
cout << endl << endl;
}
}
char GetProperty()
{ char property;
cout << "Please enter the letter corresponding to the property you want to change\n";
cout << " 'S': square Footage\n";
cout << " 'K': number of desks\n";
cout << " 'Y': number of days per week\n";
cout << " 'N': none\n";
cin >> property;
return property;
}
void DisplayClosingMessage()
{ cout << "\n\n\n H A V E F U N";
}
void DisplayIntro()
{ cout << " W E L CO M E TO O L Y M P I A ' S\n";
cout << " O F F I C E C O N T R A C T P R O G R A M\n\n\n";
}
int GetSquareFootage()
{ int sqFootage;
cout << "\nWhat is the new square footage for the contract?";
cin >> sqFootage;
return sqFootage;
}
int GetNumDesks()
{ int numDesks;
cout << "\nWhat is the new number of desks for the contract?";
cin >> numDesks;
return numDesks;
}
int GetNumDays()
{ int numDays;
cout << "\nWhat is the new number of days for the contract?";
cin >> numDays;
return numDays;
}
void ProcessUserChoices()
{
// Code to create the five instances
ContractArray contracts;
InitializeInstances(contracts);
bool done = false;
char choice;
int contractNum;
choice = GetOperatorChoice();
while (!done)
{ switch (choice)
{ case 'D':
case 'd':
contractID = GetContractID();
while (contractID != 0)
{ if ((contractID > 0) && (contractID <= MAX_CONTRACTS)) // in range?
{ DisplayInfo(contracts[contractID - 1]); // minus 1 because contracts
// go from 1 to MAX while array goes from 0 to MAX -1
}
contractID = GetContractID();
}
break; // case 'd'
case 'C':
case 'c':
contractID = GetContractID();
while (contractID != 0)
{
if ((contractID > 0) && (contractID <= MAX_CONTRACTS)) // in range?
{ ChangeInfo(contracts[contractID - 1]); // minus 1 because contracts
// go from 1 to MAX while array goes from 0 to MAX -1
}
contractID = GetContractID();
}
break; // case 'c'
case 'L':
case 'l':
DisplayAll(contracts);
break;
case 'Q':
case 'q':
cout << "Do you want to really want top quit (Y) or (N)\n";
char answ;
cin >> answ;
if ((answ =='Y') || (answ == 'y'))
{
done = true;
}
break;
default:
cout << "Invalid choice. Please try again";
choice = GetOperatorChoice();
} // end switch
if (!done)
{
choice = GetOperatorChoice();
}
}
}
void InitializeInstances(ContractArray contracts)
{ // In real program this info would be read in from a file.
// In this test code we will hard code in the initial values.
contracts[0].ChangeSquareFootage(1000);
contracts[0].ChangeNumDesks(3);
contracts[0].ChangeNumDays(5);
contracts[1].ChangeSquareFootage(500);
contracts[1].ChangeNumDesks(10);
contracts[1].ChangeNumDays(3);
contracts[2].ChangeSquareFootage(200);
contracts[2].ChangeNumDesks(1);
contracts[2].ChangeNumDays(4);
contracts[3].ChangeSquareFootage(2000);
contracts[3].ChangeNumDesks(20);
contracts[3].ChangeNumDays(5);
contracts[4].ChangeSquareFootage(300);
contracts[4].ChangeNumDesks(2);
contracts[4].ChangeNumDays(2);
}